{include file="header.htm" /}
<body class="bodystyle" style="overflow-y: scroll; cursor: default; -moz-user-select: inherit;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <a class="back_xin" href='{:weapp_url("PointsShop/PointsShop/index")}' title="返回"><i class="iconfont e-fanhui"></i></a>
            <div class="subject">
                <h3>添加积分商品</h3>
                <h5></h5>
            </div>
        </div>
    </div>

    <form class="form-horizontal" id="post_form">
        <div class="flexigrid htitx">
            <div class="ncap-form-default">
                <div class="hDiv">
                    <div class="hDivBox">
                        <table cellspacing="0" cellpadding="0" style="width: 100%">
                            <thead>
                                <tr>
                                    <th class="w10"><div class="tc"></div></th>
                                    <th class="w10"><div class="tc">交易设置</div></th>
                                    <th><div></div></th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <dl class="row">
                    <dt class="tit"><label><em>*</em>添加商品</label></dt>
                    <dd class="opt">
                        <a href="javascript:void(0);" onclick="openSelectGoods(this);" data-cancel_callback="cancelCallback" data-href="{:weapp_url('PointsShop/PointsShop/selectGoods')}" class="ncap-btn ncap-btn-green"><i class="layui-icon">&#xe608;</i>选择商品</a>
                        <p class="notic2">提示：如果原商品的规格信息产生变更，对应积分商品将自动下架，必须重新编辑积分商品的积分价方可上架</p>
                        <!-- 已选商品展示页面 -->
                        <div id="showGoods" style="display: none;">{include file="show_goods.htm" /}</div>
                        <input type="hidden" name="aidInput" id="aidInput">
                    </dd>
                </dl>

                <dl class="row">
                    <dt class="tit"><label><em>*</em>运费设置</label></dt>
                    <dd class="opt">
                        <label class="curpoin"><input name="goods_shipping" value="1" type="radio" checked="checked">读取系统设置</label>
                        &nbsp;
                        <label class="curpoin"><input name="goods_shipping" value="2" type="radio">包邮</label>
                    </dd>
                </dl>

                <div class="hDiv">
                    <div class="hDivBox">
                        <table cellspacing="0" cellpadding="0" style="width: 100%">
                            <thead>
                                <tr>
                                    <th class="w10"><div class="tc"></div></th>
                                    <th class="w10"><div class="tc">商品状态</div></th>
                                    <th><div></div></th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <dl class="row">
                    <dt class="tit"><label><em>*</em>上架设置</label></dt>
                    <dd class="opt">
                        <label class="curpoin"><input name="goods_status" value="1" type="radio" checked="checked">上架</label>
                        &nbsp;
                        <label class="curpoin"><input name="goods_status" value="2" type="radio">下架</label>
                        <p class="notic">积分商品的下架不影响原商品的上架状态；原商品如果下架或删除，积分商品自动下架并且无法上架</p>
                    </dd>
                </dl>

                <div class="bot">
                    <a href="JavaScript:void(0);" onclick="submitGoods();" class="ncap-btn-big ncap-btn-green">保存</a>
                </div>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    // 商品参数
    var aidArr = [];
    var aidStr = '';

    // 打开选择商品弹层
    function openSelectGoods(obj) {
        // 覆盖原先已选的商品ID
        var aidInput = $('#aidInput').val() ? $('#aidInput').val() : '';
        if (aidInput) {
            aidStr = aidInput;
            aidArr = aidStr.split(',');
        }
        // 打开商品弹层
        openFullframe(obj, '选择商品', '90%', '90%');
    }

    // 选择商品页点击事件
    function selectGoods(obj, delaid) {
        // 当前点击商品ID
        var aid = delaid && !obj ? String(delaid) : $(obj).attr('data-aid');
        if (aid) {
            // 数组去重
            aidArr = $.unique(aidArr.sort());
            // 判断是否已选择当前点击商品
            var index = $.inArray(aid, aidArr);
            if (-1 === parseInt(index)) {
                // 增加已选
                aidArr.push(aid);
                // 设置点击时间文案和颜色
                if (obj) $(obj).removeClass('blue').addClass('red').html('已选');
            } else if (-1 !== parseInt(index)) {
                // 去除已选
                aidArr.splice(index, 1);
                // 设置点击时间文案和颜色
                if (obj) $(obj).removeClass('red').addClass('blue').html('选择');
            }
            aidStr = aidArr.join(',');
        }
    }

    // 提交已选商品 并 加载显示商品
    function submitSelectGoods() {
        layer_loading('正在处理');
        $.ajax({
            url : "{:weapp_url('PointsShop/PointsShop/showGoods')}",
            data: {aidStr: aidStr, _ajax: 1},
            type: 'post',
            success: function(res) {
                $('#aidInput').val(aidStr);
                $('#showGoods').empty().show().html(res);
                closeSelectGoods(false);
            }
        });
    }

    // 提交已选商品 并 加载显示商品
    function deleteSelectGoods(obj) {
        layer_loading('正在处理');
        var aid = $(obj).data('aid');
        var goods_id = $(obj).data('id');
        $.ajax({
            url : "{:weapp_url('PointsShop/PointsShop/goodsDelete')}",
            data: {goods_id: goods_id, load: 0, _ajax: 1},
            type: 'post',
            success: function(res) {
                layer.closeAll();
                if (1 === parseInt(res.code)) {
                    layer.msg(res.msg, {time: 1500});
                    $(obj).parent().parent().parent().remove();
                    if (0 === parseInt($('.goodsListTr').length)) {
                        $('#aidInput').val('');
                        $('#showGoods').empty().hide();
                        closeSelectGoods(true);
                    } else {
                        selectGoods(null, aid);
                        $('#aidInput').val(aidStr);
                    }
                } else {
                    layer.alert(res.msg, {title: false, closeBtn: false});
                }
            },
            error: function(e) {
                layer.closeAll();
                layer.alert(e.responseText, {title: false, closeBtn: false});
            }
        });
    }

    // 清空已选商品 并 关闭所有弹框
    function closeSelectGoods(clear) {
        // 清空已选商品
        if (clear) {
            aidArr = [];
            aidStr = '';
        }
        // 关闭所有弹框
        layer.closeAll();
    }

    // 弹出层头部的X关闭回调
    function cancelCallback() {
        closeSelectGoods(true);
    }

    // 判断输入框是否为空
    function submitGoods() {
        layer_loading('正在处理');
        $.ajax({
            url : "{:weapp_url('PointsShop/PointsShop/add', ['_ajax'=>1])}",
            data: $('#post_form').serialize(),
            type: 'post',
            dataType: 'json',
            success: function(res) {
                layer.closeAll();
                if (1 === parseInt(res.code)) {
                    layer.msg(res.msg, {time: 1500}, function() {
                        window.location.href = "{:weapp_url('PointsShop/PointsShop/index')}";
                    });
                } else {
                    layer.alert(res.msg, {title: false, closeBtn: false}, function() {
                        layer.closeAll();
                        if (res.data.id) $(res.data.id).find('a').click();
                    });
                }
            },
            error: function(e) {
                layer.closeAll();
                layer.alert(e.responseText, {title: false, closeBtn: false});
            }
        });
    }
</script>
{include file="footer.htm" /}